package com.jugheadzhou.auth.core.websocket.heart;

import com.jugheadzhou.auth.core.websocket.WebSocketManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;

/**
 * WebSocket心跳检测任务
 *
 * @author jugheadzhou
 */
@Slf4j
public class WebSocketHeartCheckTask {

    /**
     * 心跳检测过期间隔时间
     */
    @Value("${websocket.heartCheck.expired:60000}")
    private long expired;

    @Resource
    private WebSocketManager webSocketManager;

    /**
     * 任务1 检测WebSocket连接的心跳
     */
//    @Scheduled(fixedRate = 1000 * 20)
    public void webSocketHeartCheckJob() {
        log.info("开始检测websocket连接的心跳");
        webSocketManager.check(expired, (webSockets) -> log.info("删除失去心跳的webSockets: {}", webSockets.toString()));
        log.info("websocket连接数量：{}", webSocketManager.size());
    }
}
